package com.example.tests;

import com.example.framework.ContactData;
import com.example.utils.SortedListOf;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import static com.example.utils.ContactDataGenerator.loadContactsFromCsvFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class ContactCreationTest extends TestManager {

    @DataProvider
    public Iterator<Object[]> contactsFromFile() throws IOException {
        List<ContactData> contacts = loadContactsFromCsvFile(new File("contacts.txt"));
        List<Object[]> list = convertContactToDataProviderFormat(contacts);

        return list.iterator();
    }

    @Test(dataProvider = "contactsFromFile")
    public void shouldCreateContactWithAllFields(ContactData contact){
        // save old state
        SortedListOf<ContactData> oldContactList = new SortedListOf<ContactData>(manager.getHibernateHelper().listContacts());

        // actions
        manager.getContactHelper().createContact(contact);

        // save new state
        SortedListOf<ContactData> newContactList = manager.getContactHelper().getAllContacts();

        // compare states
        assertThat(newContactList, equalTo(oldContactList.withAdded(contact)));
    }
}

